package threadpoolnio.client;

import threadpoolnio.server.javaTest01;
import threadpoolnio.util.Message;
import threadpoolnio.util.Translate;

import javax.swing.*;
import java.awt.*;
import java.net.DatagramPacket;

/**
 * LoginUI类，客户机登录界面
 *
 */
public class register extends JDialog {
    /**
     * Creates new form LoginUI
     * \
     */
    public register() {

        initComponents();
        //设置窗体位置到屏幕中央
        int x = (Toolkit.getDefaultToolkit().getScreenSize().width - this.getSize().width)/2;
        int y = (Toolkit.getDefaultToolkit().getScreenSize().height - this.getSize().height)/2;
        this.setLocation(x, y);
    }

    /**

     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblLogo = new JLabel();
        txtUserId = new JTextField();

        txtPassword = new JPasswordField();

        btnRegister = new JButton();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("用户注册");//@@@@@
        setIconImage(null);
        lblLogo.setIcon(new ImageIcon(getClass().getResource("/threadpoolnio/client/images/Login.png"))); // NOI18N

       txtUserId.setFont(new java.awt.Font("宋体", 1, 18)); // NOI18N




        btnRegister.setBackground(new java.awt.Color(16, 136, 217));
        btnRegister.setFont(new java.awt.Font("宋体", 1, 18)); // NOI18N
        btnRegister.setText("注   册");//@@@@
        btnRegister.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegisterActionPerformed(evt);
            }
        });


        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(lblLogo, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
                                                .addGap(50, 50, 50)
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                                                        .addComponent(txtUserId, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                                                                        .addComponent(txtPassword, GroupLayout.Alignment.LEADING))
                                                                .addGap(50, 50, Short.MAX_VALUE))
                                                        .addGroup(layout.createSequentialGroup()
                                                               // .addComponent(chkRemember, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                                                              //  .addGap(13, 13, 13)
                                                                //.addComponent(chkAutoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        )
                                                        .addComponent(btnRegister, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.SIZE))
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        //.addComponent(btnRegister, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                                                        //.addComponent(btnGetPassword, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                ))
                                        .addGroup(layout.createSequentialGroup()

                                                )
                                )
                              .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(47, 47, 47)
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        .addComponent(txtUserId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)

                                                )
                                                .addGap(27, 27, 27)
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        .addComponent(txtPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)

                                                )
                                                .addGap(14, 14, 14)
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                       // .addComponent(chkRemember)
                                                        //.addComponent(chkAutoLogin)
                                                ))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(26, 26, 26)
                                                .addComponent(lblLogo)))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnRegister)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                         )
                                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * 处理登录按钮点击事件的逻辑，完成登录步骤
     * @param evt 登录按钮被点击事件
     */
    private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed

            String id = txtUserId.getText();
            String password = String.valueOf(txtPassword.getPassword());
            if (id.equals("") || password.equals("")) {
                JOptionPane.showMessageDialog(null, "帐号或密码不能为空！", "错误提示", JOptionPane.ERROR_MESSAGE);
                return;
            }
            //获取服务器地址和端口

            Message msg = new Message();
            msg.setUserId(id);//登录名
            msg.setPassword(password); //密码
            msg.setType("M_LOGIN"); //登录消息类型

            //实体转二进制才能发送
            byte[] data = Translate.ObjectToByte(msg); //消息对象序列化
            //定义登录报文

            //接收服务器回送的报文
            DatagramPacket backPacket = new DatagramPacket(data, data.length);

            Message backMsg = (Message) Translate.ByteToObject(data);
            //处理登录结果

            JOptionPane.showMessageDialog(null, "注册成功\n", "注册成功", JOptionPane.ERROR_MESSAGE);
            String user=msg.getUserId();
            String pwd=msg.getPassword();
            System.out.println(user);
          System.out.println(pwd);
          javaTest01 test=new javaTest01();
          test.register(user,pwd);
          }
    /**}
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                register dialog = new register();
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables

    private JButton btnRegister;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel lblLogo;
    private JPasswordField txtPassword;
    private JTextField txtRemoteName;
    private JTextField txtRemotePort;
    private JTextField txtUserId;
    // End of variables declaration//GEN-END:variables
}
